import 'package:flutter/material.dart';
import 'package:flutter_hplus/components/tables/index.dart';
import 'package:flutter_hplus/theme/theme.dart';

/// 表格行组件
class HTableRow extends StatelessWidget {
  final Map<String, dynamic> rowData;
  final List<HDataColumn> columns;
  final List<double> columnWidths;
  final int rowIndex;
  final bool isHovered;
  final bool showStripes;
  final bool showBorder;
  final VoidCallback? onTap;

  const HTableRow({
    super.key,
    required this.rowData,
    required this.columns,
    required this.columnWidths,
    required this.rowIndex,
    required this.isHovered,
    required this.showStripes,
    required this.showBorder,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    final theme = HTheme.of(context);
    
    // 根据索引决定是否应用条纹样式
    final Color baseColor = showStripes && rowIndex % 2 == 1
        ? theme.colors.background
        : Colors.white;
    
    return Material(
      child: InkWell(
        onTap: onTap,
        splashColor: theme.colors.primary.withOpacity(0.35),
        child: Container(
          height: 48,
          color: isHovered 
              ? theme.colors.infoLight.withOpacity(0.25) 
              : baseColor,
          child: Row(
            children: columns.asMap().entries.map((colEntry) {
              final colIndex = colEntry.key;
              final column = colEntry.value;
              
              final value = rowData[column.field];
              final displayValue = column.formatValue != null 
                  ? column.formatValue!(value) 
                  : value?.toString() ?? '';
              
              return Container(
                width: columnWidths[colIndex],
                height: 48,
                padding: const EdgeInsets.symmetric(horizontal: 16.0),
                alignment: column.numeric ? Alignment.centerRight : Alignment.centerLeft,
                decoration: BoxDecoration(
                  border: Border(
                    bottom: showBorder 
                      ? BorderSide(color: theme.colors.divider)
                      : BorderSide.none,
                    right: (column.showDivider && colIndex < columns.length - 1 && showBorder)
                      ? BorderSide(color: theme.colors.divider)
                      : BorderSide.none,
                  ),
                ),
                child: column.cellBuilder != null
                  ? column.cellBuilder!(value, rowData, rowIndex)
                  : Text(
                      displayValue, 
                      style: theme.textStyles.body,
                      overflow: TextOverflow.ellipsis,
                    ),
              );
            }).toList(),
          ),
        ),
      ),
    );
  }
} 
          